/**
 * Live GPS Tracking Application
 * Geographical calculations and algorithms
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens;

import java.lang.Math;
import org.j4me.util.MathFunc;

public class GeoMath {

    public static double toRad(double deg)
    {
        return deg / 180.0 * Math.PI;
    }

    public static double distanceToLine(double lat1, double lon1, double lat2, double lon2, double lat, double lon)
    {
        //double slope = 
        //double intercept =

        return 0;
    }

    /**
     * Calculates distance between two points using the Haversine
     *  formula.
     *
     * Copyright 2002-2009 by Chris Veness
     * http://www.movable-type.co.uk/scripts/latlong.html
     */
    public static double distance(double lat1, double lon1, double lat2, double lon2)
    {
        double deltaLat = toRad(lat2 - lat1);
        double deltaLon = toRad(lon2 - lon1);

        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
                    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
                    Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);

        return 6371 * 2 * org.j4me.util.MathFunc.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    }
}
